package query.truncate

import ast.expr.SqlIdentifierExpr
import ast.statement.truncate.SqlTruncate
import database.DB
import dsl.TableSchema
import query.ReviseQuery
import util.toSqlString
import java.sql.Connection

/**
 * truncate语句dsl类
 * @property db DB 数据库类型
 * @property conn Connection? 数据库连接
 * @property isTransaction Boolean 是否是事务
 * @property sqlTruncate SqlTruncate truncate语法树
 */
class Truncate(
    var db: DB = DB.MYSQL,
    override var conn: Connection? = null,
    override var isTransaction: Boolean = false
) : ReviseQuery() {
    constructor(db: DB) : this(db, null, false)

    private var sqlTruncate = SqlTruncate()

    /**
     * truncate子句
     * 例如：db truncate "t1"
     * @param table String 表名
     * @return Truncate 清空dsl
     */
    infix fun truncate(table: String): Truncate {
        sqlTruncate.table = SqlIdentifierExpr(table)
        return this
    }

    /**
     * truncate子句
     * 例如：db truncate Table
     * @param table T 实体类伴生对象名
     * @return Truncate 清空dsl
     */
    infix fun <T : TableSchema> truncate(table: T): Truncate {
        return truncate(table.tableName)
    }

    /**
     * 生成sql
     * @return String sql语句
     */
    override fun sql() = toSqlString(sqlTruncate, db)
}